package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.ApproveDetailsBean;
import com.mingtu.guards.view.DividerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ApproveProgressAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {


    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<ApproveDetailsBean.DataBean.AuditTaskDetailEntityListBean> mData;

    public ApproveProgressAdapter(Context context, List<ApproveDetailsBean.DataBean.AuditTaskDetailEntityListBean> data) {
        this.context = context;
        this.mData = data;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mData != null) {
            if (mData.size() > 0) {
                ApproveDetailsBean.DataBean.AuditTaskDetailEntityListBean auditTaskDetailEntityListBean = mData.get(position);
                int type = Integer.parseInt(auditTaskDetailEntityListBean.getStatus()); //0:审批中 1：已通过 2：已拒绝 3：发起审批
                return type;
            }
        }

        return 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        // 实例化viewholder
        if (viewType == 0) {
            viewHolder = new ActivityHolder0(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_approve_progress0, parent, false));
        } else if (viewType == 1 || viewType == 3) {
            viewHolder = new ActivityHolder1(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_approve_progress1, parent, false));
        } else if (viewType == 2) {
            viewHolder = new ActivityHolder2(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_approve_progress2, parent, false));
        }
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (mData != null) {
            if (mData.size() > 0) {
                ApproveDetailsBean.DataBean.AuditTaskDetailEntityListBean auditTaskDetailEntityListBean = mData.get(position);
                String userName = auditTaskDetailEntityListBean.getUserName();
                String time = auditTaskDetailEntityListBean.getAuditTime(); // 审批时间
                String status = auditTaskDetailEntityListBean.getStatus(); // 状态
                String memo = auditTaskDetailEntityListBean.getAuditMemo(); //

                if (status.equals("0")) {
                    //审批中
                    if (!StringUtils.isEmpty(userName)) {
                        if (userName.length() == 1) {
                            ((ActivityHolder0) holder).tvFlag.setText(userName);
                        } else {
                            ((ActivityHolder0) holder).tvFlag.setText(userName.substring(0, 1));
                        }
                        ((ActivityHolder0) holder).tvName.setText(userName);
                    }
                    if(position == mData.size() -1){
                        ((ActivityHolder0) holder).dividerView.setVisibility(View.GONE);

                    }else{
                        ((ActivityHolder0) holder).dividerView.setVisibility(View.VISIBLE);
                    }
                } else if (status.equals("1")) {
                    //已通过
                    if (!StringUtils.isEmpty(userName)) {
                        if (userName.length() == 1) {
                            ((ActivityHolder1) holder).tvFlag.setText(userName);
                        } else {
                            ((ActivityHolder1) holder).tvFlag.setText(userName.substring(0, 1));
                        }
                        ((ActivityHolder1) holder).tvName.setText(userName);
                    }

                    if (!StringUtils.isEmpty(time)) {
                        ((ActivityHolder1) holder).tvTime.setText(time);
                    }
                    ((ActivityHolder1) holder).tvStatus.setText("同意审批");
                    ((ActivityHolder1) holder).tvStatus.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    ((ActivityHolder1) holder).tvContent.setVisibility(View.VISIBLE);
                    if (!StringUtils.isEmpty(memo)) {
                        ((ActivityHolder1) holder).tvContent.setText(memo);
                    } else {
                        ((ActivityHolder1) holder).tvContent.setText("无意见，审批通过");
                    }
                    if(position == mData.size() -1){
                        ((ActivityHolder1) holder).dividerView.setVisibility(View.GONE);
                        ((ActivityHolder1) holder).viewPlace.setVisibility(View.GONE);

                    }else{
                        ((ActivityHolder1) holder).dividerView.setVisibility(View.VISIBLE);
                        ((ActivityHolder1) holder).viewPlace.setVisibility(View.VISIBLE);
                    }
                } else if (status.equals("2")) {
                    //已拒绝
                    if (!StringUtils.isEmpty(userName)) {
                        if (userName.length() == 1) {
                            ((ActivityHolder2) holder).tvFlag.setText(userName);
                        } else {
                            ((ActivityHolder2) holder).tvFlag.setText(userName.substring(0, 1));
                        }
                        ((ActivityHolder2) holder).tvName.setText(userName);
                    }

                    if (!StringUtils.isEmpty(time)) {
                        ((ActivityHolder2) holder).tvTime.setText(time);
                    }

                    if (!StringUtils.isEmpty(memo)) {
                        ((ActivityHolder2) holder).tvContent.setText(memo);
                    } else {
                        ((ActivityHolder2) holder).tvContent.setText("有异议，审批拒绝");
                    }

                    if(position == mData.size() -1){
                        ((ActivityHolder2) holder).dividerView.setVisibility(View.GONE);

                    }else{
                        ((ActivityHolder2) holder).dividerView.setVisibility(View.VISIBLE);
                    }
                } else if (status.equals("3")) {
                    //发起审批
                    if (!StringUtils.isEmpty(userName)) {
                        if (userName.length() == 1) {
                            ((ActivityHolder1) holder).tvFlag.setText(userName);
                        } else {
                            ((ActivityHolder1) holder).tvFlag.setText(userName.substring(0, 1));
                        }
                        ((ActivityHolder1) holder).tvName.setText(userName);
                    }

                    if (!StringUtils.isEmpty(time)) {
                        ((ActivityHolder1) holder).tvTime.setText(time);
                    }

                    ((ActivityHolder1) holder).tvStatus.setText("发起审批");
                    ((ActivityHolder1) holder).tvStatus.setTextColor(context.getResources().getColor(R.color.text_color333));
                    if (!StringUtils.isEmpty(memo)) {
                        ((ActivityHolder1) holder).tvContent.setText(memo);
                        ((ActivityHolder1) holder).tvContent.setVisibility(View.GONE);
                    }

                    if(position == mData.size() -1){
                        ((ActivityHolder1) holder).dividerView.setVisibility(View.GONE);

                    }else{
                        ((ActivityHolder1) holder).dividerView.setVisibility(View.VISIBLE);
                    }
                }

            }
        }

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void upData(List<ApproveDetailsBean.DataBean.AuditTaskDetailEntityListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    private void initView() {

    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.divider_view)
        DividerView dividerView;
        @BindView(R.id.tv_flag)
        TextView tvFlag;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.view_place)
        View viewPlace;

        public ActivityHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class ActivityHolder0 extends RecyclerView.ViewHolder {
        @BindView(R.id.divider_view)
        DividerView dividerView;
        @BindView(R.id.tv_flag)
        TextView tvFlag;
        @BindView(R.id.tv_name)
        TextView tvName;

        public ActivityHolder0(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public static class ActivityHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.divider_view)
        DividerView dividerView;
        @BindView(R.id.tv_flag)
        TextView tvFlag;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_content)
        TextView tvContent;

        public ActivityHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
